<?php
if( ! function_exists('force_login') ) {
	/**
	 * Force check user login status, if not redirect to logic page at once.
	 * @param  $s_obj:  src callback obj
	 * @param  $handle: 'reirect_to_login', 'error_html', 'error_json'
	 */
	function force_login(&$s_obj, $handle="rediret_to_login", $err_msg="") {
		if( ! $s_obj->userlib->is_user() ) {
			if( strlen($err_msg)<1 )
				$err_msg = "Only login uer can access this resource.";
			if( $handle=="error_html" ) {
				flashMsg('error', "Only login user can access this resource. Please try again.");
				displayStatus('error');
			}
			elseif( $handle=="error_json" ) {
				echo json_encode( array(
					"rst" => "error",
					"error_code" => 410,  // need logic error code
					"error_msg" => $err_msg
					) );
			}
			else { //defualt to login page
				redirect("auth/login/", "location");
			}
			exit(0);
		}

		return true;
	}

}


/**
 * Header Redirect
 *
 * Header redirect in two flavors
 * For very fine grained control over headers, you could use the Output
 * Library's set_header() function.
 *
 * @access	public
 * @param	string	the URL
 * @param	string	the method: location or redirect
 * @return	string
 */
if ( ! function_exists('redirect'))
{
    function redirect($uri = '', $method = 'location', $http_response_code = 302)
    {
        /* 
        if ( ! preg_match('#^https?://#i', $uri))
        {
            $uri = site_url($uri);
        }
        */
        
        switch($method)
        {
            case 'refresh' :
                header("Refresh:0;url=".$uri);
                break;
            default :
                header("Location: ".$uri, TRUE, $http_response_code);
                break;
        }
        exit;
    }
}
?>